package com.ycsj.goldmedalnewconcept.parent.activity;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.base.OnloadingMoreListener;
import com.ycsj.common.manager.BqaManager;
import com.ycsj.common.manager.SrfManager;
import com.ycsj.common.utils.GsonUtil;
import com.ycsj.common.utils.UiUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.common.util.StateCodeUtils;
import com.ycsj.goldmedalnewconcept.parent.adapter.LessonRecodAdapter;
import com.ycsj.goldmedalnewconcept.parent.bean.LessonRecordInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 课时记录
 * Created by Administrator on 2019/3/13.
 */
public class ParentsLessonTimeRecordActivity extends BaseActivity implements View.OnClickListener, SrfManager.OnPullRefreshListener, OnloadingMoreListener {

    private ImageView ivBack;
    private RecyclerView rv;
    private SmartRefreshLayout srf;
    private RelativeLayout rlLoadingView;

    private TextView tv_title;

    //空视图
    private View emptyView;

    //数据集合
    private List<LessonRecordInfo.ClassQueryInfo> oldDatas;

    //适配器
    private LessonRecodAdapter adapter;

    //页码
    private int page;


    @Override
    protected int getContentLayout() {
        return R.layout.parent_activity_lesson_time_record;
    }

    @Override
    protected void init() {

        //沉浸状态栏
        initStateBar();

        //初始化视图
        initView();

        //初始化下来刷新控件
        initSrf();

        //初始化列表
        initRv();

        //请求数据
        http(BqaManager.NORML);

    }


    /**
     * 初始化视图
     */
    public void initView() {
        tv_title = findViewById(R.id.tv_title);
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        rv = findViewById(R.id.rv);
        srf = findViewById(R.id.srf);
        rlLoadingView = findViewById(R.id.rl_loading_view);
        rlLoadingView.setVisibility(View.VISIBLE);
        oldDatas = new ArrayList<>();

        tv_title.setText("课时记录");
    }


    /**
     * 初始化下拉刷新控件
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, this);
    }

    /**
     * 服务器数据
     */
    private void http(final int method) {

        if (method == BqaManager.NORML || method == BqaManager.PULLREFRESH) {
            page = 0;
        } else {
            page++;
        }

        Map<String, String> map = new HashMap<>();
        map.put("type", "43");
        map.put("c", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("page", page + "");

        HttpUtils.post(HttpAddress.PARENTLESSONTIMERECORD, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (null != rlLoadingView && rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {

                if (null != rlLoadingView && rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }

                if (!TextUtils.isEmpty(response)) {
                    LessonRecordInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, LessonRecordInfo.class);
                    } catch (Exception e) {
                        Log.i("test", "数据解析异常");
                    }

                    if (null != info) {
                        if ("1".equals(info.getState())) {

                            List<LessonRecordInfo.ClassQueryInfo> newDatas = info.getList();

                            if (method == BqaManager.NORML) {
                                oldDatas.clear();
                                oldDatas.addAll(newDatas);
                                adapter.notifyDataSetChanged();
                            } else {
                                oldDatas.clear();
                                BqaManager.updateRvBySmf(method, srf, rv, oldDatas, newDatas, adapter);
                                adapter.setEnableLoadMore(false);
                            }

                        } else if ("-1".equals(info.getState())) {
                            //返回-1默认没有数据
                            if (method == BqaManager.PULLREFRESH) {
                                //下拉刷时结束下拉刷新
                                srf.finishRefresh();
                            }
                        } else {
                            StateCodeUtils.showSateCode(info.getState());
                        }
                    } else {
                        Log.i("test", "返回数据为空");
                    }
                } else {
                    Log.i("test", "返回数据为空");
                }
            }
        });
    }

    /**
     * 初始化列表控件
     */
    private void initRv() {
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view, null);
        GridLayoutManager manager = new GridLayoutManager(this, 2);
        adapter = new LessonRecodAdapter(this, R.layout.parent_rv_lesson_record, oldDatas);
        BqaManager.setRv(emptyView, manager, this, adapter, rv, this);
    }


    /**
     * 下拉刷新
     *
     * @param refreshlayout
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        http(BqaManager.PULLREFRESH);
    }


    /**
     * 加载更多
     */
    @Override
    public void onLoadMore() {
        http(BqaManager.LOADINDGMORE);
    }


    /**
     * 页面控件点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        }
    }
}
